﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MappitLib;
using System.IO;

namespace Mappit {
    public partial class DialogNewTilemap : Form {
        //*********************************************************************
        #region Members
        #endregion
        //*********************************************************************
        #region Properties
        public Tileset tileset {
            get { return (Tileset)comboBoxTilesets.SelectedItem; }
        }
        public string filename {
            get { return textBoxFilename.Text.Trim(); }
        }
        #endregion
        //*********************************************************************
        #region Methods
        public DialogNewTilemap() {
            InitializeComponent();
            textBoxFilename.Select(0, "Untitled".Length);
            UpdateTilesets();
        }

        private void UpdateTilesets() {
            foreach (Tileset tileset in FormMain.Project.Tilesets) {
                int index = comboBoxTilesets.Items.Add(tileset);
                comboBoxTilesets.DisplayMember = "Filename";
            }
        }
        #endregion
        //*********************************************************************
        #region Event handlers
        private void DialogNewTilemap_FormClosing(object sender, FormClosingEventArgs e) {
            // if the user is cancelling the dialog, no need to check anything
            if (this.DialogResult == DialogResult.Cancel) {
                return;
            }
            // make sure a tilset was selected
            if (comboBoxTilesets.SelectedIndex == -1) {
                MessageBox.Show("No tileset was selected.", "Woops!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                e.Cancel = true;

            } // make sure the filename isn't blank
            else if (textBoxFilename.Text.Trim() == "") {
                MessageBox.Show("Filename cannot be left blank.", "Woops!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                e.Cancel = true;

            } // make sure the filename uses only valid characters
            else if (textBoxFilename.Text.IndexOfAny(Path.GetInvalidFileNameChars()) > 0) {
                MessageBox.Show("Filename contains invalid characters.", "Woops!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                e.Cancel = true;
            }                 
        }
        #endregion
    }
}
